package edu.nju.desserthouse.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.nju.desserthouse.action.business.VipInfoBean;
import edu.nju.desserthouse.dao.VipDao;

/**
 * Servlet implementation class VipCardServlet
 */
@WebServlet("/vipcardstatistics.manager")
public class VipCardServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
	@EJB
	VipDao vipDao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VipCardServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	public void execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		double active = 0;
		double paused = 0;
		double stopped = 0;
		VipInfoBean vipInfo = new VipInfoBean();
		vipInfo.setVipList(vipDao.getVipList());
		for(int i = 0; i < vipInfo.getListSize(); i++){
			if(vipInfo.getVipState(i + 1) == 0){
				active++;
			}else if((vipInfo.getVipState(i + 1) == 1)||(vipInfo.getVipState(i + 1) == 3)){
				paused++;
			}else if(vipInfo.getVipState(i + 1) == 2){
				stopped++;
			}else{
				System.out.println("Data Error!!!");
			}
		}
		vipInfo.setActive(Integer.parseInt(new java.text.DecimalFormat("0").format(active)));
		vipInfo.setPaused(Integer.parseInt(new java.text.DecimalFormat("0").format(paused)));
		vipInfo.setStopped(Integer.parseInt(new java.text.DecimalFormat("0").format(stopped)));
		
		double activeRatio = 0;
		double pausedRatio = 0;
		double stoppedRatio = 0;

		double total = active + paused + stopped;
		if(total != 0){
			activeRatio = (active/total) * 100 ;
			pausedRatio = (paused/total) * 100;
			stoppedRatio = (stopped/total) * 100;
		}else{
			System.out.println("No Vip!!!");
		}
		vipInfo.setActiveRatio(activeRatio);
		vipInfo.setPausedRatio(pausedRatio);
		vipInfo.setStoppedRatio(stoppedRatio);
		
		req.getSession().setAttribute("vipInfo", vipInfo);
		resp.sendRedirect("/desserthouseWEB/manager/vipcardstatistics.jsp");
	}
}
